% Optimal domestic shares and international strategy

% This code finds domestic share that maximizes re-scaled profits divided by phi^(sg-1). 
% Re-scaled profits divided by phi^(sg-1) depend on fc / (phi^(sg-1)) 
% and fx / (phi^(sg-1)), where fc is the re-scaled fixed cost of importing and 
% fx is the re-scaled fixed cost of exporting. Re-scaled profits are defined in the
% Calibration Appendix.

z= logspace(mingridz,maxgridz,gridsizez);   % grid for values of z = fc / (phi^(sg-1))
y= logspace(mingridy,maxgridy,gridsizey);   % grid for values of y = fx / (phi^(sg-1))
sh = logspace(mingrids,maxgrids,gridsizes); % grid for domestic share

% Importer-only:
[Zd, SHd] = meshgrid(z,sh);

PIM = betastuff * SHd.^(g *(1-sg)/(eps-1))  - ...
        betastuff3* (SHd.^(-1)-1).^(1/(eta*(eps-1))) .* Zd * eta*g*(sg-1);
    
[PIMopt, sDindex] = max(PIM);
SDM = sh(sDindex)'; %optimal domestic share for each point in the z grid

% Global firm:
[Zxm, SHxm, Yxm] = meshgrid(z,sh,y);

PIXM = betastuff * SHxm.^(-g *(sg-1)/(eps-1)) + ...
       betastuff2 * SHxm.^(-theta* g *(sg-1)/(eps-1)) .* Yxm.^(1-theta) / theta  - ...
      (SHxm.^(-1)-1).^(1/(eta*(eps-1))) .* Zxm * eta*g*(sg-1)*betastuff3;

[PIXMopt, sDXMindex] = max(PIXM);
sDXMindexx(:,:) = sDXMindex(1,:,:);
SDXM = sh(sDXMindexx)'; %optimal domestic share for each point in the z-y grid

% Interpolate to actual population of firms characterized by phi, fc and
% fx.

zz = fc./phi.^(param.sg-1); 
yy = fx./phi.^(param.sg-1); 

%Cap export costs;
yy = min(yy,max(y));
yy = max(yy,min(y));

if max(zz) > max(z)
    error('Interpolation of z out of range')
end
if max(yy) > max(y)
    error('Interpolation of y out of range')
end

sdM = interp1(z,SDM,zz,'spline'); % optimal domestic share conditional on importing-only
sdM = min(maxshare,sdM); sdM = max(minshare,sdM);

sdXM=interp2(z,y,SDXM,zz,yy,'spline'); % optimal domestic share conditional on global status
sdXM = min(maxshare,sdXM); sdXM = max(minshare,sdXM);

% International strategy

% Compute re-scaled profits for each status

piD = betastuff *  phi.^(sg-1); % purely domestic (non-importer and non-exporter)

piM2 = betastuff * phi.^(sg-1).* sdM.^(g *(1-sg)/(eps-1))  - ...
        betastuff3* (sdM.^(-1)-1).^(1/(eta*(eps-1))) .* fc * eta*g*(sg-1); % importer-only

piX2 = betastuff *  phi.^(sg-1) + betastuff2 * phi.^(theta*(sg-1)) .* fx.^(1-theta) / theta ; % exporter-only
    
piXM2 = betastuff *  phi.^(sg-1).* sdXM.^(-g *(sg-1)/(eps-1)) + ...
       betastuff2 * phi.^(theta*(sg-1)) .* sdXM.^(-theta* g *(sg-1)/(eps-1)) .* fx.^(1-theta) / theta  - ...
      (sdXM.^(-1)-1).^(1/(eta*(eps-1))) .* fc * eta*g*(sg-1)*betastuff3 ; % global
     
PI = [piD piM2-FM piX2-FX piXM2-FXM]; % include fixed costs to overall international strategy

checkPI = isnan(PI);
if max(max(checkPI))==1
    error('NAN entries in PI')
end
if isreal(PI)==0
    error('Complex entries in PI')        
end

[PRO,I] = max(PI'); % I gives optimal international status with, 
% 1 denoting purely-domestic, 2 denoting importer-only, 3 denoting 
% exporter-only, and 4 denoting global

S = I';
    
% Optimal domestic shares:
sD = (S==1) + (S==2) .* sdM + (S==3) + (S==4) .*sdXM;

% Associated export share:
sX = (1 +  phi.^(-(sg-1)*(theta-1)) .* sD .^(g / (eps-1)* (sg-1)*(theta-1)) .*  beta ^ (-g * eps / (eps-1) * (sg-1) * (theta-1)) .* fx.^(theta-1)).^(-1) ;
sX = ((S==3) + (S==4)) .* sX;